<?php
/**
 * Project: AIGC
 * Author: moufer <moufer@163.com>
 * Date: 2024/7/6 09:50
 */

namespace module\ai\logic;

use module\ai\library\channel\Factory;
use module\ai\model\Key;
use mof\Logic;
use mof\Searcher;
use think\Paginator;

class KeyLogic extends Logic
{
    /**
     * @var Key 模型
     */
    protected $model;

    public function paginate(Searcher $searcher, bool $simple = false): Paginator
    {
        $paginate = parent::paginate(...func_get_args());
        $paginate->getCollection()->each(function ($item) {
            $item->config_txt = Factory::key($item->flag)->formattingKeyTitle($item->config);
        });
        return $paginate;
    }

    public function getModelByFlag($flag): Key
    {
        $model = $this->model->where('flag', $flag)->find();
        if (!$model) {
            $model = $this->model->newInstance([
                'flag' => $flag,
            ]);
        }
        return $model;
    }
}